package admin

import (
	"gitee.com/gopher2011/gin"
	"gitee.com/gopher2011/mall/app/dao"
	"gitee.com/gopher2011/mall/app/service"
	"net/http"
	"strconv"
)

// 限时购和商品关系管理 /flashProductRelation
var SmsFlashPromotionProductRelation = &smsFlashPromotionProductRelationApi{}

type smsFlashPromotionProductRelationApi struct{}

// 批量选择商品添加关联 /create Post 参数: dao.SmsFlashPromotionProductRelation
func (it *smsFlashPromotionProductRelationApi) Create(r *gin.Context) {
	// 将请求参数解析到 ptr上
	var ptr dao.SmsFlashPromotionProductRelation
	err := r.Parse(&ptr)
	if err != nil {
		r.JSON(http.StatusBadRequest, gin.H{"msg": err.Error()})
		r.Abort()
	}
	res, err := service.SmsFlashPromotionProductRelation.Create(ptr)
	if err != nil {
		r.JSON(http.StatusBadRequest, gin.H{"msg": "批量选择商品添加关联失败!"})
		r.Abort()
	} else {
		r.JSON(http.StatusBadRequest, gin.H{"msg": "批量选择商品添加关联成功!", "data": res})
	}
}

// 修改关联相关信息 /update/:id Post 参数:id,dao.SmsFlashPromotionProductRelation
func (it *smsFlashPromotionProductRelationApi) Update(r *gin.Context) {
	// 将请求参数解析到 ptr上
	str := r.Param("id")
	id, _ := strconv.Atoi(str)
	var ptr dao.SmsFlashPromotionProductRelation
	err := r.Parse(&ptr)
	if err != nil {
		r.JSON(http.StatusBadRequest, gin.H{"msg": err.Error()})
		r.Abort()
	}
	res, err := service.SmsFlashPromotionProductRelation.Update(int64(id), ptr)
	if err != nil {
		r.JSON(http.StatusBadRequest, gin.H{"msg": "修改关联相关信息失败!"})
		r.Abort()
	} else {
		r.JSON(http.StatusBadRequest, gin.H{"msg": "修改关联相关信息成功!", "data": res})
	}
}

// 删除关联 /delete/:id Get 参数:id
func (it *smsFlashPromotionProductRelationApi) Delete(r *gin.Context) {
	str := r.Param("id")
	id, _ := strconv.Atoi(str)
	res, err := service.SmsFlashPromotionProductRelation.Delete(int64(id))
	if err != nil {
		r.JSON(http.StatusBadRequest, gin.H{"msg": "删除关联失败!"})
		r.Abort()
	} else {
		r.JSON(http.StatusBadRequest, gin.H{"msg": "删除关联成功!", "data": res})
	}
}

//获取管理商品促销信息 /:id Get 参数:id
func (it *smsFlashPromotionProductRelationApi) GetItem(r *gin.Context) {
	str := r.Param("id")
	id, _ := strconv.Atoi(str)
	res, err := service.SmsFlashPromotionProductRelation.GetItem(int64(id))
	if err != nil {
		r.JSON(http.StatusBadRequest, gin.H{"msg": "获取管理商品促销信息失败!"})
		r.Abort()
	} else {
		r.JSON(http.StatusBadRequest, gin.H{"msg": "获取管理商品促销信息成功!", "data": res})
	}
}

// 分页查询不同场次关联及商品信息 /list Get 参数 dao.SmsFlashPromotionProductRelationList 返回值:dao.SmsFlashPromotionProduct
func (it *smsFlashPromotionProductRelationApi) List(r *gin.Context) {
	// 将请求参数解析到 ptr上
	var ptr dao.SmsFlashPromotionProductRelationList
	err := r.Parse(&ptr)
	if err != nil {
		r.JSON(http.StatusBadRequest, gin.H{"msg": err.Error()})
		r.Abort()
	}
	res, err := service.SmsFlashPromotionProductRelation.List(ptr)
	if err != nil {
		r.JSON(http.StatusBadRequest, gin.H{"msg": "分页查询不同场次关联及商品信息失败!"})
		r.Abort()
	} else {
		r.JSON(http.StatusBadRequest, gin.H{"msg": "分页查询不同场次关联及商品信息成功!", "data": res})
	}
}
